// gfreebox - A Freebox remote control
//
// Copyright (C) 2011 Laurent BRULET
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your option)
// any later version.
// 
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#include "RemoteWindow.h"
#include "FreeboxInt.h"
#include "ConfigurationManager.h"

#include <cstdio>

void RemoteWindow::connectButton(Gtk::Button *p_button, std::string key, bool longPress)
{
    RemoteWindow::Key k;
    k.key = key;
    k.longPress = longPress;
    k.repeat = 1;

    p_button->signal_clicked().connect(sigc::bind<RemoteWindow::Key>(
                sigc::mem_fun(
                    *this,
                    &RemoteWindow::OnButtonClicked
                    ),
                k)
            );
}

Gtk::Button *RemoteWindow::createButton(int left, int top, const char *text, const char*key, bool longPress)
{
    Gtk::Button *p_button = new Gtk::Button(text);
    m_buttonList.push_back(p_button);
    connectButton(p_button, key, longPress);
    m_container.attach(*p_button, left, left + 1, top, top + 1);
    p_button->show();

    return p_button;
}

RemoteWindow::RemoteWindow():
    m_container(10, 3, true)
{
    Gtk::Button *p_b;

    add(m_container);

    createButton(0, 0, "1", "1", true);
    createButton(1, 0, "2", "2", true);
    createButton(2, 0, "3", "3", true);
    createButton(0, 1, "4", "4", true);
    createButton(1, 1, "5", "5", true);
    createButton(2, 1, "6", "6", true);
    createButton(0, 2, "7", "7", true);
    createButton(1, 2, "8", "8", true);
    createButton(2, 2, "9", "9", true);

    p_b = createButton(1, 3, "^", "up");
    p_b = createButton(0, 4, "<", "left");
    p_b = createButton(1, 4, "OK", "ok");
    p_b = createButton(2, 4, ">", "right");
    p_b = createButton(1, 5, "v", "down");

    createButton(0, 7, "Info", "info");
    createButton(1, 7, "Free", "home");

    p_b = createButton(1, 8, "", "red");
    p_b->modify_bg(Gtk::STATE_NORMAL, Gdk::Color("red"));
    p_b->modify_bg(Gtk::STATE_ACTIVE, Gdk::Color("dark red"));
    p_b->modify_bg(Gtk::STATE_PRELIGHT, Gdk::Color("red"));
    p_b = createButton(0, 9, "", "yellow");
    p_b->modify_bg(Gtk::STATE_NORMAL, Gdk::Color("yellow"));
    p_b->modify_bg(Gtk::STATE_ACTIVE, Gdk::Color("#808000"));
    p_b->modify_bg(Gtk::STATE_PRELIGHT, Gdk::Color("yellow"));
    p_b = createButton(2, 9, "", "green");
    p_b->modify_bg(Gtk::STATE_NORMAL, Gdk::Color("green"));
    p_b->modify_bg(Gtk::STATE_ACTIVE, Gdk::Color("dark green"));
    p_b->modify_bg(Gtk::STATE_PRELIGHT, Gdk::Color("green"));
    p_b = createButton(1, 10, "", "blue");
    p_b->modify_bg(Gtk::STATE_NORMAL, Gdk::Color("blue"));
    p_b->modify_bg(Gtk::STATE_ACTIVE, Gdk::Color("dark blue"));
    p_b->modify_bg(Gtk::STATE_PRELIGHT, Gdk::Color("blue"));

    p_b = createButton(0, 8, "V+", "vol_inc");
    p_b = createButton(0, 10, "V-", "vol_dec");
    p_b = createButton(2, 8, "P+", "prgm_inc");
    p_b = createButton(2, 10, "P-", "prgm_dec");

    createButton(0, 11, "Play", "play");
    createButton(1, 11, "Stop", "stop");

    m_p_entryCode = new Gtk::Entry();
    m_p_entryCode->signal_changed().connect(sigc::mem_fun(*this, &RemoteWindow::OnNewCode));
    m_p_entryCode->set_text(m_p_cm->GetCode());
    m_container.attach(*m_p_entryCode, 0, 3, 12, 13);
    m_p_entryCode->show();

    m_container.show();
}

RemoteWindow::~RemoteWindow()
{
}

void RemoteWindow::OnButtonClicked(RemoteWindow::Key k)
{
    //printf("%s\n", k.key.c_str());
    m_p_freeboxInt->SendCommand(k.key.c_str(), k.longPress, k.repeat);
}

void RemoteWindow::SetFreeboxInt(FreeboxInt *p_freeboxInt)
{
    m_p_freeboxInt = p_freeboxInt;
}

void RemoteWindow::SetConfigurationManager(ConfigurationManager *p_cm)
{
    m_p_cm = p_cm;
}

void RemoteWindow::OnNewCode()
{
    printf("code changed : %s\n", m_p_entryCode->get_text().c_str());
    m_p_cm->SetCode(m_p_entryCode->get_text());
}
